// credits/pages/exchange-record/index.js
import {
  creditsIconMap
} from '../../config/common'
import Dialog from '../../../libs/@vant/weapp/dialog/dialog';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentValue: '',
    tabs: [{
        label: "全部",
        value: "",
      },
      {
        label: "门店确认",
        value: "1",
      },
      {
        label: "平台确认",
        value: "2",
      },
      {
        label: "审核通过",
        value: "3",
      },
      {
        label: "门店作废",
        value: "4",
      },
      {
        label: "平台作废",
        value: "5",
      },
    ],
    auditShow: false,
    auditItem: {},
    actions: [{
          name: '通过',
          color: '#169BD5'
      },
      {
          name: '作废',
          color: 'rgb(238, 10, 36)'
      },
    ],
  },

  onTabClick(event) {
    this.setData({
      currentValue: event.currentTarget.dataset.value
    }, () => {
      this.refreshCom.refresh()
    })
  },
  requestData() {
    return new Promise((resolve, reject) => {
      wx.ajax
        .post('pageQueryScoreApply.action', {
          type: this.data.currentValue,
        }).then(res => {
          res.result.pageList.forEach(item => {
            item.icon = creditsIconMap['3'].icon
            item.statusName = this.tabMap[item.status]
          })
          resolve(res)
        }).catch(reject)
    })
  },
  refreshPage() {
    this.refreshCom.refresh()
  },
  onItemEvent(event) {
    const {
      eventName,
      item
    } = event.detail
    // 待供应商审核的可以审核
    console.log(item.status);
    if (eventName == 'itemClick' && item.status == '1') {
        console.log(333);
      this.setData({
        auditShow: true,
        auditItem: item
      })
    } else if (eventName == 'remove') {
       
    }
  },
  onClose() {
    this.setData({
      auditShow: false
    });
  },
  onSelect({detail}) {
    const { name } = detail
    let status = name == '通过' ? '2' : '4'
    wx.ajax.post('checkScoreApply.action', { id: this.data.auditItem.id, status }).then(() => {
      wx.showToast({
        title: '操作成功',
      })
      this.refreshPage()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      tabs
    } = this.data
    const tabMap = {}
    tabs.forEach(item => {
      tabMap[item.value] = item.label
    })
    this.tabMap = tabMap

    this.refreshCom = this.selectComponent('#listRefresh')
    this.setData({
      currentValue: options.type || '',
      requestFun: this.requestData
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})